package com.hs.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.Articletype;
import com.hs.sfzy.service.ArticletypeService;
import com.hs.sfzy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ArticletypeController
 * @Description TODO
 * @Author shanglin
 * @Date 2024年10月24日 19:48
 * @Version 1.0
 */
@RestController
@RequestMapping("/articletype")
public class ArticletypeController {
    @Autowired
    private ArticletypeService articletypeService;

    @PostMapping
    public Result insert(@RequestBody Articletype articletype) {
        int num = articletypeService.add(articletype);
        if (num != 0) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    /**
     * 根据id删除数据
     *
     * @param id
     * @return com.zgy.sfzy.util.Result
     * @author zgy
     * @date 2024/10/21 15:29
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") int id) {
        try {
            int num = articletypeService.delete(id);
            if (num != 0) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            return Result.error("删除失败");
        }

    }

    @PutMapping
    public Result update(@RequestBody Articletype articletype) {
        int num = articletypeService.update(articletype);
        if (num != 0) {
            return Result.success("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }

    /**
     * 查询所有数据
     *
     * @param articletype
     * @return com.zgy.sfzy.util.Result
     * @author zgy
     * @date 2024/10/21 15:29
     */
    @GetMapping
    public Result selectAll(Articletype articletype) {
        List<Articletype> list = articletypeService.selectAll(articletype);
        return Result.success(list);
    }

    /**
     * 根据id查询数据
     *
     * @return com.zgy.sfzy.util.Result
     * @author zgy
     * @date 2024/10/21 15:31
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") int id) {
        Articletype articletype = articletypeService.selectById(id);
        return Result.success(articletype);
    }

    /**
     * 分页查询数据
     *
     * @param pageNum
     * @param pageSize
     * @param articletype
     * @return com.zgy.sfzy.util.Result
     * @author zgy
     * @date 2024/10/21 16:06
     */
    @GetMapping("/findByPage")
    public Result selectByPage(
            @RequestParam(required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "5") int pageSize,
            Articletype articletype) {
        PageInfo<Articletype> info = articletypeService.selectByPage(pageNum, pageSize, articletype);
        return Result.success(info);
    }

}
